package com.sunday.minio.spring.e3_spring_mvc;

import com.sunday.minio.core.MinioTemplate;
import com.sunday.minio.entity.BatchWriteResponse;
import com.sunday.minio.entity.SingleWriteResponse;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.Enumeration;
import java.util.Map;

@Slf4j
@DependsOn("minioDataSourceHandler")
@RestController
@RequestMapping("/api")
public class ApiController {

    @Autowired
    private MinioTemplate minioTemplate;

    /**
     * 单文件上传
     *
     * @param request
     * @return {@link String}
     */
    @PostMapping("/put")
    public String put(HttpServletRequest request) {
        Enumeration<String> enumeration = request.getHeaderNames();
        while (enumeration.hasMoreElements()) {
            String key = enumeration.nextElement();
            log.info("HEAD : {} ==== {}", key, request.getHeader(key));
        }

        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> multipartFileMap = multipartHttpServletRequest.getFileMap();
        multipartFileMap.forEach((name, multipartFile) -> {
            log.info("{}", name);
//            log.info("MultipartFile name : {}, originalFilename : {} ,ContentType : {}", v.getName(), v.getOriginalFilename(), v.getContentType());
            log.info("name : {}, originalFilename : {}, ContentType : {}", multipartFile.getName(), multipartFile.getOriginalFilename(), multipartFile.getContentType());
            SingleWriteResponse singleWriteResponse = minioTemplate.put(multipartFile);
            log.info("{}", singleWriteResponse);
            log.info("上传后返回的重要信息 target -> {}", singleWriteResponse.format());
        });
        return "minioTemplate.put(multipartFile)";
    }

    /**
     * 批量上传
     *
     * @param request
     * @return {@link String}
     */
    @PostMapping("/uploadMultipartFileSnowball")
    public String uploadMultipartFileSnowball(HttpServletRequest request) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> multipartFileMap = multipartHttpServletRequest.getFileMap();
        multipartFileMap.forEach((k, v) -> {
            log.info("name : {}", k);
            log.info("MultipartFile name : {}, originalFilename : {}, ContentType : {}", v.getName(), v.getOriginalFilename(), v.getContentType());
        });
        BatchWriteResponse batchWriteResponse = minioTemplate.uploadMultipartFileSnowball(multipartFileMap.values());
        batchWriteResponse.getBaseResults().stream().forEach(result -> log.info("上传后返回的重要信息 target -> {}", result.format()));
        return batchWriteResponse.toString();
    }

}
